//
//  DKGlobalHelper.m
//  DanKU
//
//  Created by 吴斌 on 2023/10/7.
//

#import "DKGlobalHelper.h"

@implementation DKGlobalHelper
static DKGlobalHelper *defaultManager = nil;
+ (instancetype)defaultManager{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        if(defaultManager == nil){
            defaultManager = [[self alloc] init];
        }
    });
    return defaultManager;
}
-(void)setUserRsaKey:(NSString *)userRsaKey{
    _userRsaKey = userRsaKey;
    DK_SETObject_USERDEF(NSUserDefault_UserRsaKey, _userRsaKey);
}
- (NSString *)getUserRsaKey{
    if(_userRsaKey.length > 0){
        return _userRsaKey;
    }else{
        NSString *defUserRsaKey = DK_GETObject_USERDEF(NSUserDefault_UserRsaKey);
        if(defUserRsaKey.length > 0){
            _userRsaKey = defUserRsaKey;
            return _userRsaKey;
        }else{
            return @"";
        }
    }
}

+ (void)saveRegisterUserInfo:(DKRegisterUserModel*)registerUserMdl{
    if (registerUserMdl) {
        [DKGlobalHelper defaultManager].registerUserMdl = registerUserMdl;
        //对象无法存储到缓存，需要归档
        NSData *userRegisterData = [NSKeyedArchiver archivedDataWithRootObject:registerUserMdl requiringSecureCoding:false error:nil];
        DK_SETObject_USERDEF(NSUserDefault_UserRegister, userRegisterData);
    }
}
+ (DKRegisterUserModel *)getRegisterUserInfo{
    if([DKGlobalHelper defaultManager].registerUserMdl){
        return [DKGlobalHelper defaultManager].registerUserMdl;
    }else{
        NSData *userRegisterData = DK_GETObject_USERDEF(NSUserDefault_UserRegister);
        DKRegisterUserModel *registerUserMdl = [NSKeyedUnarchiver unarchiveObjectWithData:userRegisterData];
        if (registerUserMdl) {
            [DKGlobalHelper defaultManager].registerUserMdl = registerUserMdl;
            return registerUserMdl;
        }
    }
    return nil;
}
+ (void)loginOutApp{
    [DKGlobalHelper defaultManager].registerUserMdl = nil;
    [DKGlobalHelper defaultManager].loanSetpInfoMdl = nil;
    [DKGlobalHelper defaultManager].userBaseInfoMdl = nil;
    [DKGlobalHelper defaultManager].userComplaintMdl = nil;
    DK_Remove_USERDEF(NSUserDefault_UserToken);
    DK_Remove_USERDEF(NSUserDefault_UserRsaKey);
    DK_Remove_USERDEF(NSUserDefault_UserRegister);
}
- (NSString *)phoneRandomNum{
    if(_phoneRandomNum.length){
        return _phoneRandomNum;
    }else{
        [DKGlobalHelper defaultManager].phoneRandomNum = [NSString getRandomStringWithNum:16];
        return _phoneRandomNum;
    }
}
@end
